package com.health.viewscontrols.lvyuejl;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.health.R;
import com.health.adapter.LvYueListAdapter;
import com.health.model.LvYueFuWuBean;
import com.health.model.PageData;
import com.health.tablebean.ARCHIVE_BASEINFO;

import com.health.tools.GlobalConts;
import com.health.tools.SPUtil;
import com.health.viewscontrols.NovaActivity;
import com.health.viewscontrols.NovaApplication;
import com.nova.BaseFragmentActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 履约记录列表
 * Created by John on 2019/3/4.
 */

public class LvYueListActivity extends NovaActivity {
    Button btn_back;//返回
    TextView title;//标题
    Button add;//添加
    ARCHIVE_BASEINFO userinfo = null;
    private GridView gd_da;
    private Button btn_top;
    private Button btn_next;

    private TextView tv_allpage;
    private TextView tv_cupage;

    private PageData nd;
    private List<LvYueFuWuBean> infos = new ArrayList<>();
    private int pageall = 1;
    private int pagesize = 9;

    LvYueListAdapter lvYueListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lvyuelist);
        userinfo = NovaApplication.getUserInfo();
        findViews();
        setListeners();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setDatasByType();
    }

    @Override
    public void findViews() {
        btn_back=findViewById(R.id.btn_back);
        title=findViewById(R.id.title);
        title.setText("履约服务列表");
        add=findViewById(R.id.add);
        gd_da =findViewById(R.id.gd_da);
        btn_top = findViewById(R.id.btn_top);
        btn_next = findViewById(R.id.btn_next);

        tv_allpage = findViewById(R.id.tv_allpage);
        tv_cupage = findViewById(R.id.tv_cupage);
    }

    private void setDatasByType() {
        if (infos == null) {
            infos = new ArrayList<>();
        }
        nd = new PageData();
        // nd.setCurrentPage("1");
        nd.setCountall(pageall);
        nd.setCountcurrent(1);


        int count = 0;
        List<LvYueFuWuBean> baseinfos = NovaApplication.fdbmanger.findAllByWhere(LvYueFuWuBean.class,
                "cardNum='"+userinfo.getIDCARD()+"'");
        count=baseinfos.size();
        Log.w("eeeeeeeeeeepagcount", "" + count);
        if (count > 0) {

            pageall = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
            nd.setCountall(pageall);
            nd.setCountcurrent(1);
            getPageData(1);
        } else {
            nd = new PageData();
            // nd.setCurrentPage("1");
            nd.setCountall(pageall);
            nd.setCountcurrent(1);
            tv_allpage.setText("");
            tv_cupage.setText("");
            gd_da.setAdapter(null);
        }
        setButtonBg();

        gd_da.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String str_jddwno = SPUtil.getStrjddwno(LvYueListActivity.this);
                Log.e("建档单位",str_jddwno);
                if("632223".equals(str_jddwno.substring(0,6))){
                    Intent intent=new Intent(LvYueListActivity.this,ShowLvYueActivity1.class);
                    intent.putExtra("LvYueBean",infos.get(position));
                    startActivity(intent);
                }else{
                    Intent intent=new Intent(LvYueListActivity.this,ShowLvYueActivity.class);
                    intent.putExtra("LvYueBean",infos.get(position));
                    startActivity(intent);
                }



            }
        });
//        gd_da.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
//
//            @Override
//            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
//                if (GlobalContsJTYS.FRAGMENT_MEMBER_INTENT_ID_MAIN.equals(strIntentId)) {
//
//                    // IS_UPDATE //Y 未上传 N已上传
//                    if ("Y".equals(infos.get(position).getIS_UPDATE())) {
//                        showDeleteDialog(position);
//                    }
//                }
//                return true;
//            }
//        });
    }

    /**
     * 获取分页数据
     *
     *
     * @author 王婷婷
     * @date 2018年1月16日 下午7:08:17
     */
    private void getPageData(int currentPage) {

        tv_allpage.setText("总页数：" + pageall);
        tv_cupage.setText("当前页：" + currentPage);
        Log.w("eeeeeeeeeeepageall", "" + pageall);

        infos = (List<LvYueFuWuBean>) NovaApplication.dbManger.getLvYueList( currentPage,
                   userinfo.getIDCARD());


        if (infos.size() > 0) {
            lvYueListAdapter = new LvYueListAdapter( infos, LvYueListActivity.this,gd_da);
            gd_da.setAdapter(lvYueListAdapter);
            lvYueListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setListeners() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_jddwno = SPUtil.getStrjddwno(LvYueListActivity.this);
                Log.e("建档单位",str_jddwno);
                if("632223".equals(str_jddwno.substring(0,6))){
                    Intent intent=new Intent(LvYueListActivity.this,LvYueJiLuActivity1.class);
                    startActivity(intent);
                }else{
                    Intent intent=new Intent(LvYueListActivity.this,LvYueJiLuActivity.class);
                    startActivity(intent);
                }




            }
        });

        btn_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                preData();
            }
        });
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextData();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void preData() {
        // 上一页
        if (nd.getCountcurrent() > nd.getCountall()) {
            nd.setCountcurrent(nd.getCountall());
        }

        if (nd.getCountcurrent() == 1) {
            showTextDialog("已经是第一页了");
            btn_top.setBackground(getResources().getDrawable(R.drawable.btn_top2));
        } else {
            nd.setCountcurrent(nd.getCountcurrent() - 1);
            infos = new ArrayList<>();
            gd_da.setAdapter(null);
            getPageData(nd.getCountcurrent());
        }
        setButtonBg();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void nextData() {

        if (nd.getCountcurrent() > nd.getCountall()) {
            nd.setCountcurrent(nd.getCountall());
        }

        if (nd.getCountcurrent() == nd.getCountall()) {
            showTextDialog("已经是最后一页了");
            btn_next.setBackground(getResources().getDrawable(R.drawable.btn_bottom2));
        } else {
            nd.setCountcurrent(nd.getCountcurrent() + 1);
            infos = new ArrayList<>();
            gd_da.setAdapter(null);
            getPageData( nd.getCountcurrent());
        }

        setButtonBg();
    }

    /**
     * 设置背景色
     *
     * @author 王婷婷
     * @date 2016年2月22日 上午11:21:24
     */
    @SuppressLint("NewApi")
    private void setButtonBg() {
        if (nd.getCountcurrent() == 1) {
            if(nd.getCountall()<=1){
                btn_top.setBackground(getResources().getDrawable(R.drawable.btn_top2));
                btn_next.setBackground(getResources().getDrawable(R.drawable.btn_bottom2));
            }else{
                btn_top.setBackground(getResources().getDrawable(R.drawable.btn_top2));
                btn_next.setBackground(getResources().getDrawable(R.drawable.btn_bottom1));
            }
        } else if (nd.getCountcurrent() == nd.getCountall()) {
            btn_top.setBackground(getResources().getDrawable(R.drawable.btn_top1));
            btn_next.setBackground(getResources().getDrawable(R.drawable.btn_bottom2));
        } else if ((nd.getCountcurrent() > 1) && (nd.getCountcurrent() < nd.getCountall())) {
            btn_top.setBackground(getResources().getDrawable(R.drawable.btn_top1));
            btn_next.setBackground(getResources().getDrawable(R.drawable.btn_bottom1));
        }
    }

}
